/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.api;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.storage.common.SmsTemplate;
import com.jeesite.modules.storage.common.SmsType;
import com.jeesite.modules.storage.service.SmsService;
import com.jeesite.modules.storage.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 短信Controller
 */
@RestController(value = "apisms")
@RequestMapping(value = "/api/sms")
public class SMSController extends APIController {

    @Autowired
    private SmsService smsService;

    /**
     * 发送验证码
     * @param type 类型
     * @param template 短信模板
     * @param phone 手机号
     * @return 发送结果
     */
    private ServerResponse<String> send(String type , String template , String phone)
    {
        String msg = this.smsService.sendCode(type , template , phone);
        return StringUtils.isBlank(msg)?ServerResponse.createBySuccess(200,"发送成功!"):ServerResponse.createBySuccess(500,msg);
    }

    /**
     * 发送注册验证码
     * @param phone 手机号
     * @return 发送结果
     * 200 发送成功
     * 500 其它
     */
    @RequestMapping(method = RequestMethod.POST,value = "register")
    public ServerResponse<String> register(@RequestParam String phone)
    {
        return send(SmsType.PHONE_REGISTER_TYPE , SmsTemplate.PHONE_REGISTER_TEMPLATE , phone);
    }

    /**
     * 发送登录验证码
     * @param phone 手机号
     * @return 发送结果
     * 200 发送成功
     * 500 其它
     */
    @RequestMapping(method = RequestMethod.POST,value = "login")
    public ServerResponse<String> login(@RequestParam(value="phone") String phone)
    {
        return send(SmsType.PHONE_LOGIN_TYPE , SmsTemplate.PHONE_LOGIN_TEMPLATE , phone);
    }

    /**
     * 发送绑定验证码
     * @param phone 手机号
     * @return 发送结果
     * 200 发送成功
     * 500 其它
     */
    @RequestMapping(method = RequestMethod.POST,value = "bind")
    public ServerResponse<String> bind(@RequestParam(value="phone") String phone)
    {
        return send(SmsType.PHONE_BIND_TYPE , SmsTemplate.PHONE_BIND_TEMPLATE , phone);
    }
}
